{extend name="public/world"}
{block name="style"}{/block}
{block name="container"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/html" id="barDemo">
{if isset($sonAuth['editType'])}
    <input type="checkbox" name="visable" value="{{ d.visable }}" lay-skin="switch" lay-filter="switchStatus" lay-text="启用|禁用" {{ d.visable == 1 ? 'checked' : '' }} data-href="{:url('/erp.Advert/editType')}?id={{ d.id }}">
    <a href="{:url('/erp.Advert/editType')}?id={{ d.id }}" class="layui-btn layui-btn-xs this-pop" lxb-data="{width: '650px', height: '80%'}" title="修改类别">修改</a>
{/if}
{if isset($sonAuth['addType'])}
    {{# if(d.pid == 0){ }}
    <a href="{:url('/erp.Advert/addType')}?pid={{ d.id }}" class="layui-btn layui-btn-xs this-pop" lxb-data="{width: '650px', height: '80%'}" title="添加子类别">添加子类别</a>
    {{# } }}
{/if}
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group fl">
        {if isset($sonAuth['addType'])}
        <a href="{:url('/erp.Advert/addType')}" class="layui-btn layui-btn-xs this-pop" lxb-data="{width: '650px', height: '80%'}" title="添加类别">添加类别</a>
        {/if}
    </div>
</script>
<script>
    layui.use(['table'], function() {
        var $ = layui.$,
            table = layui.table;
        $(document).on('click', '.this-pop', function() {
            var that = $(this), query = '';

            var def = {width: '600px', height: '460px',maxmin:true, idSync: false, table: 'dataTable', type: 2, url: that.attr('href'), title: that.attr('title'), btn1: '确定', btn2: '取消',close:false};
            var opt = new Function('return '+ that.attr('lxb-data'))() || {};

            opt.url     = opt.url || def.url;
            opt.title   = opt.title || def.title;
            opt.width   = opt.width || def.width;
            opt.height  = opt.height || def.height;
            opt.type    = opt.type || def.type;
            opt.table   = opt.table || def.table;
            opt.idSync  = opt.idSync || def.idSync;
            opt.btn1  = opt.btn1 || def.btn1;
            opt.btn2  = opt.btn2 || def.btn2;
            opt.close  = opt.close || def.close;
            opt.maxmin = opt.maxmin || def.maxmin;
            opt.btns = [opt.btn1, opt.btn2];
            opt.shadeClose = false;
            if(opt.btn1 == 'no') {
                opt.btns = false;
                opt.shadeClose = true;
            }
            if(opt.title == 'no') opt.title = false;
            if(opt.maxmin == 'no') opt.maxmin = false;

            if (!opt.url) {
                layer.msg('请设置href参数');
                return false;
            }

            if (opt.url.indexOf('?') >= 0) {
                opt.url += '&lxb_iframe=yes'+query;
            } else {
                opt.url += '?lxb_iframe=yes'+query;
            }

            layer.open({
                type: opt.type
                ,title: opt.title
                ,content: opt.url
                ,maxmin: opt.maxmin
                ,area: [opt.width, opt.height]
                ,btn: opt.btns
                ,shadeClose:opt.shadeClose
                ,btnAlign: 'c' //按钮居中
                ,fixed: false //不固定
                ,moveOut: true //true  可以拖出窗外  false 只能在窗内拖
                ,anim: 5 //出场动画 isOutAnim bool 关闭动画
                ,offset: 'auto' //['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
                ,shade: .7 //遮罩
                ,resize: true //是否允许拉伸
                ,move: '.layui-layer-title' // 默认".layui-layer-title",// 触发拖动的元素

                ,success: function(layero, index){
                    layero.find('.layui-layer-min').remove();
                    if(opt.close) layero.find('.layui-layer-close2').addClass('layui-layer-close1').removeClass('layui-layer-close2');
                }
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-front-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        layer.load();
                        $.ajax({
                            type: 'POST',
                            url: opt.url,
                            data: field,
                            dataType: "json",
                            success:function(res) {
                                if(res.msg) layer.msg(res.msg);
                                if (res.code == 0) {
                                    layer.closeAll('loading');
                                }
                                if (res.code == 1) {
                                    table.reload(opt.table); //数据刷新
                                    setTimeout(function() {
                                        layer.closeAll(); //关闭弹层
                                    }, 1500);
                                }
                                if (res.code == 2) {
                                    setTimeout(function(){
                                        if (typeof(res.url) != 'undefined' && res.url != null && res.url != '') {
                                            window.location.href = res.url;
                                        } else {
                                            window.location.reload();
                                        }
                                        layer.closeAll();
                                    }, 1500);
                                }
                                if (res.code == 3) {
                                    setTimeout(function(){
                                        layer.closeAll(); //关闭弹层
                                    }, 1500);
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
            return false;
        });
        var tableIns = table.render({
            elem: '#dataTable',
            url: "{:url('/erp.Advert/types')}",
            method: 'post',
            toolbar: '#toolbar',
            defaultToolbar: false,
            cols: [
                [
                    { field: 'id', title: 'ID', width: 60, fixed: 'left'}
                    , {
                        field: 'name',
                        title: '类别名称',
                        templet: function(res) {
                            if(res.pid==0)
                                return '<span class="role-name1">' + res.name + '</span>';
                            else
                                return '<span class="role-name2">' + res.name + '</span>';
                        }
                    }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 260 }
                ]
            ],
            page: false
        });
    });
</script>
{/block}